﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections;
using System.Windows;

namespace FieldTools.Client.Command
{
    /// <summary>
    /// 窗口管理器
    /// </summary>
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public static void Register<T>(string key)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, typeof(T));
            }
        }
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        public static void Register(string key, Type t)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, t);
            }
        }
        /// <summary>
        /// 移除注册的窗口
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }
        /// <summary>
        /// 窗口显示
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        public static Window? Show(string key, object? VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                //var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                var win = (Window)App.serviceProvider.GetRequiredService((Type)_RegisterWindow[key]);
                if (win != null)
                {
                    win.Closed += Win_Closed;
                    if(VM != null)
                    {
                        win.DataContext = VM;
                    }
                    var isOpened = win.GetType()?.GetProperty("IsOpened")?.GetValue(win, null);
                    if (isOpened != null)
                    {
                        if ((bool)isOpened)
                        {
                            MessageBox.Show("窗口已打开，请勿重新打开");
                            return win;
                        }
                    }
                    win.GetType()?.GetProperty("IsOpened")?.SetValue(win, true);
                    win.Show();
                    return win;
                }
                else
                {
                    return null;
                }
            }
            return null;
        }
        /// <summary>
        /// 获取窗口实体
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        /// <returns></returns>
        public static Window? GetWindow(string key, object? VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                //var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                var win = (Window)App.serviceProvider.GetRequiredService((Type)_RegisterWindow[key]);
                if (win != null)
                {
                    if (VM != null)
                    {
                        win.DataContext = VM;
                    }
                    return win;
                }
                else
                {
                    return null;
                }
            }
            return null;
        }

        private static void Win_Closed(object? sender, EventArgs e)
        {
            sender?.GetType()?.GetProperty("IsOpened")?.SetValue(sender, false);
            GC.Collect();
        }
    }
}
